package com.happok.onlineclass.usermanagement.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.happok.common.exception.BusinessException;
import com.happok.onlineclass.usermanagement.setting.dao.FunctionSettingDAO;
import com.happok.onlineclass.usermanagement.setting.dto.FunctionClassifyEnum;
import com.happok.onlineclass.usermanagement.setting.dto.UpdateVerifyDTO;
import com.happok.onlineclass.usermanagement.setting.po.FunctionSettingPO;
import com.happok.onlineclass.usermanagement.setting.service.FunctionSettingDaoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 接口分类                        yapi 分类文件夹
 * 功能说明
 *
 * @author : chenac
 * @module 项目名称/模块名          yapi 项目组/子项目
 * @date : 2021/10/12 12:19
 */
@Service
public class FunctionSettingDaoServiceImpl implements FunctionSettingDaoService {

    @Autowired
    private FunctionSettingDAO functionSettingDAO;

    @Override
    public void openOrCloseFunction(FunctionClassifyEnum functionClassify, Boolean open) {
        notExistCreate(functionClassify);
        functionSettingDAO.functionEnable(functionClassify, open);
    }

    @Override
    public void updateFunctionSetting(FunctionClassifyEnum functionClassify, UpdateVerifyDTO updateVerify) {
        if (!ObjectUtils.isEmpty(updateVerify.getEmailVerify()) &&
                !ObjectUtils.isEmpty(updateVerify.getEmailVerify().getContent())) {
            switch (functionClassify) {
                case RESET_PASSWORD:
                    if (!updateVerify.getEmailVerify().getContent().contains("${password}")) {
                        throw new BusinessException("参数异常,content必须包含${password}字段");
                    }
                    break;
                case ADD_USER:
                    if (!updateVerify.getEmailVerify().getContent().contains("${password}") &&
                            !updateVerify.getEmailVerify().getContent().contains("${account}") ) {
                        throw new BusinessException("参数异常,content必须包含${password}、${account}字段");
                    }
                    break;
                default:
                    if (!updateVerify.getEmailVerify().getContent().contains("${code}")) {
                        throw new BusinessException("参数异常,content必须包含${code}字段");
                    }
                    break;
            }
        }
        notExistCreate(functionClassify);
        LambdaUpdateWrapper<FunctionSettingPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(FunctionSettingPO::getFunctionClassify, functionClassify);
        FunctionSettingPO functionSettingPO = new FunctionSettingPO();
        if (!ObjectUtils.isEmpty(updateVerify.getEmailVerify())) {
            BeanUtils.copyProperties(updateVerify.getEmailVerify(), functionSettingPO);
        }
        if (!ObjectUtils.isEmpty(updateVerify.getSmsVerify())) {
            BeanUtils.copyProperties(updateVerify.getSmsVerify(), functionSettingPO);
        }
        functionSettingDAO.update(functionSettingPO, updateWrapper);
    }

    @Override
    public FunctionSettingPO getFunctionSetting(FunctionClassifyEnum functionClassify) {
        LambdaQueryWrapper<FunctionSettingPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FunctionSettingPO::getFunctionClassify, functionClassify);
        return functionSettingDAO.selectOne(queryWrapper);
    }

    @Override
    public List<FunctionSettingPO> listFunctionSetting() {
        LambdaQueryWrapper<FunctionSettingPO> queryWrapper = new LambdaQueryWrapper<>();
        return functionSettingDAO.selectList(queryWrapper);
    }

    private void notExistCreate(FunctionClassifyEnum functionClassify) {
        FunctionSettingPO functionSettingPO = getFunctionSetting(functionClassify);
        if (ObjectUtils.isEmpty(functionSettingPO)) {
            functionSettingPO = new FunctionSettingPO();
            functionSettingPO.setFunctionClassify(functionClassify);
            if (FunctionClassifyEnum.USER_REGIST.equals(functionClassify)) {
                functionSettingPO.setOpen(false);
            }
            functionSettingDAO.insert(functionSettingPO);
        }
    }
}